<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/11/8
 * Time: 9:53
 */

namespace app\admin\controller;


use think\Db;

class Order extends Base
{
    /**
     * 订单列表展示
     * @return mixed|\think\response\Json
     */
    public function index()
    {
        if(request()->isAjax()) {
            $params = input('param.');
            $where = [];
            //订单号查询
            if(isset($params['order_num']) && $params['order_num'] != ''){
                $where['uo.order_num'] = ['like', "%" . $params['order_num'] . "%"];
            }
            //酒店名称查询
            if(isset($params['hotel_name']) && $params['hotel_name'] != ''){
                $where['uh.h_name'] = ['like', "%" . $params['hotel_name'] . "%"];
            }
            //客户姓名查询
            if(isset($params['user_username']) && $params['user_username']){
                $where['uuu.user_username'] = ['like', "%" . $params['user_username'] . "%"];
            }
            //手机号查询
            if(isset($params['user_phone']) && $params['user_phone']){
                $where['uuu.user_phone'] = ['like', "%" . $params['user_phone'] . "%"];
            }
            $page = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            //列表
            $orderList = Db::table('up_order')
                ->alias('uo')
                ->join('up_u_user uuu' , 'uuu.user_id = uo.user_id', 'left')
                ->join('up_hotel uh', 'uh.h_id = uo.hotel_id', 'left')
                ->join('up_room ur', 'ur.r_id = uo.room_id', 'left')
                ->field('ur.r_num, uh.h_name, uuu.user_username, uuu.user_phone, uo.order_num, 
            uo.check_time, uo.leave_time, uo.create_time, uo.pay_price, uo.cash_price, 
            uo.order_status, uo.par_time, uo.is_consume, uo.is_cash')
                ->where($where)
                ->page($page, $limits)
                ->order('uo.create_time desc')
                ->select();
            //总记录
            $count = $this->countList($where);
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$orderList]);
        }
        return $this->fetch("order/index");
    }
    
    /**
     * 统计总记录数
     * @param $where
     * @return int|string
     */
    public function countList($where){
        $count = Db::table('up_order')
            ->alias('uo')
            ->join('up_u_user uuu' , 'uuu.user_id = uo.user_id', 'left')
            ->join('up_hotel uh', 'uh.h_id = uo.hotel_id', 'left')
            ->join('up_room ur', 'ur.r_id = uo.room_id', 'left')
            ->where($where)
            ->count();
        return $count;
    }
}